<template>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="100px"
    >
      <el-form-item label="类型" prop="type">
        <el-radio-group v-model="dataForm.type">
          <el-radio
            v-for="(type, index) in dataForm.typeList"
            :label="index"
            :key="index"
            @change="changeRadio(index)"
            >{{ type }}</el-radio
          >
        </el-radio-group>
      </el-form-item>
      <el-form-item
        :label="dataForm.typeList[dataForm.type] + '名称'"
        prop="menuName"
      >
        <el-input
          v-model="dataForm.menuName"
          :placeholder="dataForm.typeList[dataForm.type] + '名称'"
        ></el-input>
      </el-form-item>
      <el-form-item label="上级菜单">      
        <el-popover
          ref="menuListPopover"
          placement="bottom-start"
          popper-class="menuListPopover"
          trigger="click"
          v-model="visible1"
        >
          <el-tree
            :data="menuList"
            :props="menuListTreeProps"
            class="menudrop"
            node-key="menuId"
            ref="menuListTree"
            @current-change="menuListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false"
          >
          </el-tree>
        </el-popover>
        <el-input
          v-model="dataForm.parentName"
          v-popover:menuListPopover
          @click.native="menuListPopover"
          :readonly="true"
          placeholder="点击选择上级菜单"
          class="menu-list__input"
        ></el-input>
      </el-form-item>
      <el-form-item
        v-if="dataForm.type === 1"
        label="规则组"
        prop="groupIdentifys"
      >
        <el-select
          placeholder="请选择规则组"
          multiple
          v-model="dataForm.department"
          @change="getSelectDep"
        >
          <el-checkbox
            :style="selfstyle"
            v-model="checkedThing"
            @change="selectAllThing"
            >全选</el-checkbox
          >
          <el-option
            v-for="item in departments"
            :label="`${item.value}-${item.label}`"
            :value="item.value"
            :key="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-if="dataForm.type !== 2" label="排序号" prop="orderNum">
        <el-input-number
          v-model="dataForm.orderNum"
          controls-position="right"
          :min="0"
          label="排序号"
        ></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button :plain="true" type="info" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { treeDataTranslate } from "@/utils";
import { getGroupList, menuInfoSave } from "@/api/rulesmenu";
import Icon from "@/icons";
export default {
  data() {
    var validateUrl = (rule, value, callback) => {
      if (this.dataForm.type === 1 && !/\S/.test(value)) {
        callback(new Error("规则组不能为空"));
      } else {
        callback();
      }
    };
    return {
      title: "",
      visible: false,
      visible1: false,
      departments: [],
      checkedThing: false,

      selfstyle: {
        textAlign: "right",
        width: "100%",
        paddingRight: "10px",
      },
      dataForm: {
        menuId: null,
        type: 1,
        typeList: ["目录", "菜单"],
        menuName: "",
        parentId: 0,
        parentName: "",
        orderNum: 0,
        remark: null,
        groupIdentifys: "",
        department: [],
      },
      dataRule: {
        menuName: [
          { required: true, message: "菜单名称不能为空", trigger: "blur" },
        ],
        // parentName: [
        //   { required: true, message: "上级菜单不能为空", trigger: "change" },
        // ],
        groupIdentifys: [{ validator: validateUrl, trigger: "change" }],
      },
      menuList: [],
      menuListTreeProps: {
        label: "menuName",
        children: "children",
      },
    };
  },
  created() {
    this.getGroupLists();
  },
  methods: {
    changeRadio(index) {
      // console.log(index, "222");
      if (index === 0) {
        this.dataForm.department = [];
        this.dataForm.groupIdentifys = "";
      }
    },
    menuListPopover() {
      // console.log(this.$refs.menuListPopover);
      this.$nextTick(() => {
        document.querySelector('.menuListPopover').style.width = `${document.querySelector('.menu-list__input').offsetWidth - 26}px`;
      });
    },
    getGroupLists() {
      getGroupList({}).then(({ data }) => {
        // console.log(data);
        if (data.resultCode === 0) {
          this.departments = [];
          data.data.forEach((e, i) => {
            this.departments[i] = {
              label: e.groupName,
              value: e.groupIdentify,
            };
          });
        }
      });
    },
    getSelectDep(department) {
      if (department.length === this.departments.length) {
        this.checkedThing = true;
      } else {
        this.checkedThing = false;
      }
      this.dataForm.groupIdentifys = this.dataForm.department.join(",");
      // console.log(this.dataForm.department, "222");
    },

    selectAllThing() {
      this.dataForm.department = [];
      if (this.checkedThing) {
        this.departments.map((item) => {
          this.dataForm.department.push(item.label);
        });
      } else {
        this.dataForm.department = [];
      }
      this.dataForm.groupIdentifys = this.dataForm.department.join(",");
    },

    selectRules() {},
    CheckedRuleGroup() {},
    // 菜单树选中
    menuListTreeCurrentChangeHandle(data, node) {
      this.dataForm.parentId = data.menuId;
      this.dataForm.parentName = data.menuName;
      this.visible1 = false

    },
    // 菜单树设置当前选中节点
    menuListTreeSetCurrentNode() {
      this.$refs.menuListTree.setCurrentKey(this.dataForm.parentId);
      this.dataForm.parentName = (this.$refs.menuListTree.getCurrentNode() ||
        {})["menuName"];
    },
    // 表单提交
    dataFormSubmit() {
      // console.log(this.dataForm.type);
      if (this.dataForm.type === 0) {
        this.dataForm.department = [];
        this.dataForm.groupIdentifys = "";
      }
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          menuInfoSave(this.dataForm).then(({ data }) => {
            // console.log(data);
            if (data && data.resultCode === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>

<style lang="scss">
.menudrop {
  max-height: 500px;
  overflow: auto;
}
.mod-menu {
  .menu-list__input,
  .icon-list__input {
    > .el-input__inner {
      cursor: pointer;
    }
  }
  &__icon-popover {
    width: 458px;
    overflow: hidden;
  }
  &__icon-inner {
    width: 478px;
    max-height: 258px;
    overflow-x: hidden;
    overflow-y: auto;
  }
  &__icon-list {
    width: 458px;
    padding: 0;
    margin: -8px 0 0 -8px;
    > .el-button {
      padding: 8px;
      margin: 8px 0 0 8px;
      > span {
        display: inline-block;
        vertical-align: middle;
        width: 18px;
        height: 18px;
        font-size: 18px;
      }
    }
  }
  .icon-list__tips {
    font-size: 18px;
    text-align: center;
    color: #e6a23c;
    cursor: pointer;
  }
}
</style>
